//
//  OrdetailViewModel.m
//  xingfugangwan
//
//  Created by rainday on 15/1/5.
//  Copyright (c) 2015年 rainday. All rights reserved.
//
#import "OrdetailViewModel.h"
#import <AFNetworking.h>
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "NetIP.h"
#define LIST @[@[@"新郎",@"新娘",@"结婚时间",@"结婚地址",@"联系人",@"联系人电话",@"负责人",@"主题",@"色调",@"预付款",@"全款",@"签名"],@[@"新郎",@"新娘",@"回门时间",@"回门地址",@"联系人",@"联系人电话",@"负责人",@"主题",@"色调",@"预付款",@"全款",@"签名"],@[@"新郎",@"新娘",@"结婚时间",@"结婚地址",@"回门时间",@"回门地址",@"联系人",@"联系人电话",@"负责人",@"主题",@"色调",@"预付款",@"全款",@"签名"],@[@"主角",@"宴会时间",@"宴会地址",@"联系人",@"联系人电话",@"负责人",@"主题",@"色调",@"预付款",@"全款",@"签名"],@[@"主角",@"宴会时间",@"宴会地址",@"联系人",@"联系人电话",@"负责人",@"主题",@"色调",@"预付款",@"全款",@"签名"]]

@interface OrdetailViewModel(){
    id _orderid;
    NSDictionary *_info;
    
    NSArray *_infoArray;
    
    NSArray *_dataArray;
}
@end

@implementation OrdetailViewModel

- (id)initWithOrderId:(id)orderid{
    self = [super init];
    if (self) {
        _orderid = orderid;
        _info = [NSDictionary dictionary];
        
        _dataArray = [NSArray array];
    }
    
    return self;
}

- (RACSignal *)fetchOrderDataWithOrderId:(id)orderId{
    
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id token = [userDefaults objectForKey:@"token"];
        
        
        NSDictionary *postDic = [NSDictionary dictionaryWithObjectsAndKeys:_orderid,@"orderid", nil];
        
        
        
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        
        [manager POST:[NSString stringWithFormat:@"%s/signing/detail/",IPSTRING] parameters:postDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([[responseObject objectForKey:@"state"] isEqualToString:@"success"]) {
                
               
                _info = [responseObject objectForKey:@"info"];
                self.state = [_info objectForKey:@"status"];
                
                id nature =[_info objectForKey:@"party_nature"];
                if ([nature isEqualToString:@"结婚"]) {
                    _infoArray = LIST[0];
                }
                if ([nature isEqualToString:@"回门"]) {
                    _infoArray = LIST[1];
                }
                if ([nature isEqualToString:@"合办"]) {
                    _infoArray = LIST[2];
                }
                if ([nature isEqualToString:@"生日"]) {
                    _infoArray = LIST[3];
                }
                if ([nature isEqualToString:@"其他"]) {
                    _infoArray = LIST[4];
                }
                
                _dataArray = [responseObject objectForKey:@"items"];
                
                [subscriber sendNext:@"success"];
                
                
                
            }else{
                [subscriber sendNext:@"failed"];
                
            }
            
            
            
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            
        }];
        
        
        return nil;
    }];
    
}

- (NSInteger)numerOfSection
{
    if ([_dataArray count]>0) {
        return 2;
    }
    return 1;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    
    if (section==0) {
        NSArray *array = [_info allKeys];
        if([array count]>0){
            return [_infoArray count];
            
        }
        return 0;
    }
    return [_dataArray count];
    
}

- (NSString *)titleForRowInIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
       return _infoArray[indexPath.row];
    }
    return _dataArray[indexPath.row];
}

- (NSString *)titleForHeaderOfSection:(NSInteger)section
{
    if (section ==0) {
        id orderid = [_info objectForKey:@"orderID"];
        
        return [NSString stringWithFormat:@"订单号:%@",orderid];
        
    }
    
    return [_dataArray count]==0?@"暂无订单项目":@"订单项目";
    
}

- (NSString *)detailForRowInIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0 ) {
        if ([_infoArray count]>indexPath.row && ![_infoArray[indexPath.row] isEqual:[NSNull null]]) {
            
            NSLog(@"%@",_infoArray[indexPath.row]);
            if ([_infoArray[indexPath.row] isEqual:@"新郎"] || [_infoArray[indexPath.row] isEqual:@"主角"]) {
                return [_info objectForKey:@"role1"];
            }
            
            if ([_infoArray[indexPath.row] isEqual:@"新娘"]) {
                return [_info objectForKey:@"role2"];
            }
            
            if ([_infoArray[indexPath.row] isEqual:@"结婚时间"] || [_infoArray[indexPath.row] isEqual:@"宴会时间"]) {
                return [_info objectForKey:@"weddingTime"];
            }
            
            if ([_infoArray[indexPath.row] isEqual:@"结婚地址"] || [_infoArray[indexPath.row] isEqual:@"宴会地址"]) {
                return [_info objectForKey:@"weddingAddress"];
            }
            if ([_infoArray[indexPath.row] isEqual:@"联系人"]) {
                return [[_info objectForKey:@"client"] objectForKey:@"name"];
            }
            if ([_infoArray[indexPath.row] isEqual:@"联系人电话"] ) {
                return [[_info objectForKey:@"client"] objectForKey:@"phone"];
            }
            if ([_infoArray[indexPath.row] isEqual:@"负责人"] ) {
                return [[_info objectForKey:@"saleman"] objectForKey:@"name"];
            }
            if ([_infoArray[indexPath.row] isEqual:@"主题"] ) {
                return [_info objectForKey:@"theme"];
            }
            if ([_infoArray[indexPath.row] isEqual:@"色调"] ) {
                return [_info objectForKey:@"hue"];
            }
            if ([_infoArray[indexPath.row] isEqual:@"预付款"] ) {
                return [NSString stringWithFormat:@"%d", [[_info objectForKey:@"advancePay"] intValue]];
            }
            if ([_infoArray[indexPath.row] isEqual:@"全款"] ) {
                if ([_info objectForKey:@"pirce"] ==nil) {
                    return @"0";
                }
                return [NSString stringWithFormat:@"%d", [[_info objectForKey:@"pirce"] intValue]];
            }
//            if ([_infoArray[indexPath.row] isEqualToString:@"签名"] ) {
//                if ([_info objectForKey:@"signImage"] ==nil) {
//                    return @"无";
//                }
//                return [_info objectForKey:@"signImage"];
//            }
        }
    }
    
    return @"";
}
@end
